package com.hotel.repository;

import com.hotel.config.DatabaseConfig;
import com.hotel.entity.Menu;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MenuRepository {

    public MenuRepository() {}

    public void save(Menu menu) {
        String sql = "insert into MenuItems(item_name,price,category) values (?,?,?)";
        try (Connection conn = DriverManager.getConnection(DatabaseConfig.URL, DatabaseConfig.USER, DatabaseConfig.PASSWORD);
             PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
            stmt.setString(1, menu.getItem_name());
            stmt.setBigDecimal(2, menu.getPrice());
            stmt.setString(3, menu.getCategory());
            stmt.executeUpdate();
            try (ResultSet rs = stmt.getGeneratedKeys()) {
                if (rs.next()) {
                    menu.setItem_id(rs.getInt(1));
                }
            }
        } catch (SQLException e) {
            throw new RuntimeException("菜品添加失败", e);
        }
    }

    public List<Map<String, Object>> findAll() {
        String sql = "select * from menuitems";
        List<Map<String, Object>> menus = new ArrayList<>();
        try (Connection conn = DriverManager.getConnection(DatabaseConfig.URL, DatabaseConfig.USER, DatabaseConfig.PASSWORD);
             Statement stmt = conn.createStatement()) {
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                Map<String, Object> menu = new HashMap<>();
                menu.put("item_id",rs.getInt("item_id"));
                menu.put("item_name",rs.getString("item_name"));
                menu.put("price",rs.getBigDecimal("price"));
                menu.put("category",rs.getString("category"));
                menus.add(menu);
            }
        } catch (SQLException e) {
            throw new RuntimeException("查询失败", e);
        }
        return menus;
    }
    public void delete(Integer id) {
        String sql = "delete from menuitems where item_id = ?";
        try (Connection conn = DriverManager.getConnection(DatabaseConfig.URL, DatabaseConfig.USER, DatabaseConfig.PASSWORD);
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, id);
            stmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException("删除失败", e);
        }
    }
    public void update(int id,Menu menu) {
        String sql = "update menuitems set item_name = ?, price = ?, category = ? where item_id = ?";
        try (Connection conn = DriverManager.getConnection(DatabaseConfig.URL, DatabaseConfig.USER, DatabaseConfig.PASSWORD);
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setString(1, menu.getItem_name());
            stmt.setBigDecimal(2, menu.getPrice());
            stmt.setString(3, menu.getCategory());
            stmt.setInt(4, id);
            stmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException("更新失败", e);
        }
    }

}
